package com.xx.cloud.pay.controller;


import com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.xx.cloud.pay.entity.CreateOrderEntity;
import com.xx.cloud.pay.result.R;
import com.xx.cloud.pay.service.PayUnifyService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 通用接口支付
 */
@RestController
@RequestMapping("/pay")
@CrossOrigin("*")
public class PayUnifyController {

    @Autowired
    private PayUnifyService payUnifyService;

    /**
     * 下单接口
     * @param createOrderEntity
     * @return
     * @throws WxPayException
     */
    @PostMapping("/create")
    public R createOrder(@RequestBody CreateOrderEntity createOrderEntity) throws WxPayException {
        R order = payUnifyService.createOrder(createOrderEntity);
        return order;
    }


    /**
     * * TODO 异步回调V3
     * * @param notifyData
     * * @param request
     *
     * @throws WxPayException
     */
    @PostMapping("/notify/orderV3")
    public String parseOrderNotifyResultPersonV3(@RequestBody String notifyData, HttpServletRequest request) throws WxPayException {
        return payUnifyService.parseOrderNotifyResultPersonV3(notifyData, request);

    }


    /**
     * V3退款
     *
     * @param request
     * @return
     * @throws WxPayException
     */
    @PostMapping("/refundV3")
    public R refundV3(@RequestBody WxPayRefundV3Request request) throws WxPayException {
        return payUnifyService.refundV33(request);
    }


    /**
     * 关闭
     * @param request
     * @return
     */
    @GetMapping("/closeOrderV3")
    public R<?> closeOrderV3(@RequestBody WxPayUnifiedOrderV3Request request) {
        return payUnifyService.closeOrderV33(request);
    }


}
